/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package commons.util;

import java.util.Properties;

import javax.xml.bind.PropertyException;

/**
 * @author Susanta Tewari
 * @since Aug 20, 2011
 */
public class ValidationUtil {

    /**
     * Validates that the key exists in the specified properties object and has a non-empty value.
     *
     * @param props a properties object used in the validation
     * @param key the property key being validated
     * @throws PropertyException if the key does not exist or has an empty value
     * @deprecated use {@link #validateProperty(String, String)} instead
     */
    public static void validateProperty(Properties props, String key) throws PropertyException {

        if (props.getProperty(key) == null) {
            throw new PropertyException("Property " + key + " does not exist.");
        }

        if (props.getProperty(key).isEmpty()) {
            throw new PropertyException("Property " + key + " is empty.");
        }

    }


    /**
     * Validates that the key value is not {@code null} or empty.
     *
     * @param key key
     * @param value key value
     * @throws PropertyException if the key value is {@code null} or empty
     */
    public static void validateProperty(String key, String value) throws PropertyException {

        if (value == null) {
            throw new PropertyException("Value for property key: " + key + " does not exist.");
        }

        if (value.isEmpty()) {
            throw new PropertyException("Value for property key: " + key + " is empty.");
        }

    }
}
